﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

using Microsoft.Practices.Unity;

namespace Happy.Unity
{
    /// <summary>
    /// PerRequest生命周期管理。
    /// </summary>
    public sealed class PerRequestLifetimeManager : LifetimeManager
    {
        private readonly Guid _key;

        /// <summary>
        /// 构造方法。
        /// </summary>
        public PerRequestLifetimeManager()
        {
            _key = Guid.NewGuid();
        }

        /// <inheritdoc />
        public override object GetValue()
        {
            return HttpContext.Current.Items[_key];
        }

        /// <inheritdoc />
        public override void SetValue(object newValue)
        {
            HttpContext.Current.Items[_key] = newValue;
        }

        /// <inheritdoc />
        public override void RemoveValue()
        {
            HttpContext.Current.Items.Remove(_key);
        }
    }
}
